package com.changcheng.project.obupublish.controller;

import java.util.List;

import com.changcheng.common.TextUtils;
import com.changcheng.project.obupublishlog.domain.AObuPublishLog;
import com.changcheng.project.obupublishlog.service.IAObuPublishLogService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.changcheng.framework.aspectj.lang.annotation.Log;
import com.changcheng.framework.aspectj.lang.enums.BusinessType;
import com.changcheng.project.obupublish.domain.AObuPublish;
import com.changcheng.project.obupublish.service.IAObuPublishService;
import com.changcheng.framework.web.controller.BaseController;
import com.changcheng.framework.web.domain.AjaxResult;
import com.changcheng.common.utils.poi.ExcelUtil;
import com.changcheng.framework.web.page.TableDataInfo;

/**
 * obu发布Controller
 *
 * @author dazhi
 * @date 2020-10-30
 */
@Controller
@RequestMapping("/obupublish/obupublish")
public class AObuPublishController extends BaseController {
    private String prefix = "obupublish/obupublish";

    @Autowired
    private IAObuPublishService aObuPublishService;
    @Autowired
    private IAObuPublishLogService aObuPublishLogService;


    @RequiresPermissions("obupublish:obupublish:view")
    @GetMapping()
    public String obupublish() {
        return prefix + "/obupublish";
//        6
    }



    /**
     * 查询obu发布列表
     */
    @RequiresPermissions("obupublish:obupublish:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(AObuPublish aObuPublish) {
        startPage();
        List<AObuPublish> list = aObuPublishService.selectAObuPublishList(aObuPublish);

        return getDataTable(list);
    }

    /**
     * 导出obu发布列表
     */
    @RequiresPermissions("obupublish:obupublish:export")
    @Log(title = "obu发布", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(AObuPublish aObuPublish) {
        List<AObuPublish> list = aObuPublishService.selectAObuPublishList(aObuPublish);
        ExcelUtil<AObuPublish> util = new ExcelUtil<AObuPublish>(AObuPublish.class);
        return util.exportExcel(list, "mybatis/obupublish");
    }

    /**
     * 新增obu发布
     */
    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }

    int zhi;

    /**
     * 新增保存obu发布
     */
    @RequiresPermissions("obupublish:obupublish:add")
    @Log(title = "obu发布", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(AObuPublish aObuPublish) {


        if (!TextUtils.isBlank(aObuPublish.getObuPublishFactory()) &&
                !TextUtils.isBlank(aObuPublish.getObuPublishName()) &&
                !TextUtils.isBlank(aObuPublish.getObuPublishTime()) &&
                !TextUtils.isBlank(aObuPublish.getObuPublishUrl()) &&
                !TextUtils.isBlank(aObuPublish.getObuPublishVersion())

        ) {
            AObuPublish AObuPublishs = new AObuPublish();
            AObuPublishs.setObuPublishFactory(aObuPublish.getObuPublishFactory());
            List<AObuPublish> list = aObuPublishService.selectAObuPublishList(AObuPublishs);
            if (list != null && list.size() > 0) {
                zhi = -1;
                return toAjax2(zhi, "只能存在一条发布记录，请删除原有记录后添加！");
            } else {
                //小于0
                zhi = aObuPublishService.insertAObuPublish(aObuPublish);
                AObuPublishLog  AObuPublishLogs=new AObuPublishLog();
                AObuPublishLogs.setObuPublishFactory(aObuPublish.getObuPublishFactory());
                AObuPublishLogs.setObuPublishName(aObuPublish.getObuPublishName());
                AObuPublishLogs.setObuPublishTime(aObuPublish.getObuPublishTime());
                AObuPublishLogs.setObuPublishUrl(aObuPublish.getObuPublishUrl());
                AObuPublishLogs.setObuPublishVersion(aObuPublish.getObuPublishVersion());
                aObuPublishLogService.insertAObuPublishLog(AObuPublishLogs);
                return toAjax(zhi);
            }


        } else {
            zhi = -1;
            return toAjax2(zhi, "数据不能为空！");
        }
    }

    /**
     * 修改obu发布
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap) {
        AObuPublish aObuPublish = aObuPublishService.selectAObuPublishById(id);
        mmap.put("aObuPublish", aObuPublish);
        return prefix + "/edit";
    }

    /**
     * 修改保存obu发布
     */
    @RequiresPermissions("obupublish:obupublish:edit")
    @Log(title = "obu发布", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(AObuPublish aObuPublish) {
        return toAjax(aObuPublishService.updateAObuPublish(aObuPublish));
    }

    /**
     * 删除obu发布
     */
    @RequiresPermissions("obupublish:obupublish:remove")
    @Log(title = "obu发布", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(aObuPublishService.deleteAObuPublishByIds(ids));
    }
}
